package comwork.example.halou.day_encrydemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.rock.teach.utils.encrypt.DES;
import com.rock.teach.utils.encrypt.MD5;
import com.rock.teach.utils.encrypt.RSA;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private String mDESkey;
    private String mRsaKeyPrivaty;//私钥
    private String mRsaKeyPublic;//公钥

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.et_content);
        try {
            mDESkey= DES.initKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Map<String, Object> map = RSA.initKey();
//            获取私钥和公钥
            
            mRsaKeyPrivaty=RSA.getPrivateKey(map);
            mRsaKeyPublic=RSA.getPublicKey(map);
            Log.e("", "onCreate:公钥 "+mRsaKeyPublic+"私钥"+mRsaKeyPrivaty );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void myClick(View view) {
        switch (view.getId()) {
            case R.id.jiami:
                String trim = editText.getText().toString().trim();
                try {
//                    加密的一个过程
                    String encode = URLEncoder.encode(trim, "utf-8");
                    editText.setText(encode);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.jiemi:
//                进行揭秘
                try {
                    String decode = URLDecoder.decode(editText.getText().toString().trim(), "utf-8");
                    editText.setText(decode);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.basee:
                byte[] bytes = editText.getText().toString().trim().getBytes();
                String s = Base64.encodeToString(bytes, Base64.CRLF);
                editText.setText(s);

                break;
            case R.id.based:
                byte[] bytes1 = editText.getText().toString().trim().getBytes();
                byte[] decode = Base64.decode(bytes1, Base64.CRLF);
                String s1 = new String(decode);
                editText.setText(s1);
                break;
            case R.id.btn_md5:

//                不可逆的
                byte[] bytes2 = editText.getText().toString().trim().getBytes();
                try {
                    String s2 = MD5.encryptMD5ToString(bytes2);
                    editText.setText(s2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
//            对成加密
            case R.id.btn_desjia:
                byte[] bytes3 = editText.getText().toString().trim().getBytes();
                try {
                    byte[] encrypt = DES.encrypt(bytes3, mDESkey);
//                    加密后的数组再进行base64编码
                    String s2 = Base64.encodeToString(encrypt, Base64.DEFAULT);
                    editText.setText(s2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case
                    R.id.btn_desjie:
//                揭秘的过程
                byte[] decode1 = Base64.decode(editText.getText().toString().trim(), Base64.DEFAULT);
                try {
                    byte[] decrypt = DES.decrypt(decode1, mDESkey);
                    editText.setText(new String(decrypt));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
//            对称加密
            case R.id.btn_rsajia:
                byte[] bytes4 = editText.getText().toString().trim().getBytes();
                try {
                    byte[] bytes5 = RSA.encryptByPrivateKey(bytes4, mRsaKeyPrivaty);
                    String s2 = Base64.encodeToString(bytes5, Base64.DEFAULT);
                    editText.setText(s2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_rsajie:
                byte[] decode2 = Base64.decode(editText.getText().toString().trim(), Base64.DEFAULT);
                try {
                    byte[] bytes5 = RSA.decryptByPublicKey(decode2, mRsaKeyPublic);
                    editText.setText(new String(bytes5));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
